package com.kingmed.kmss.admin.modules.ums.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingmed.kmss.admin.modules.ums.model.UmsKmcsOrg;
import com.kingmed.kmss.admin.modules.ums.service.UmsKmcsOrgService;
import com.kingmed.kmss.admin.modules.ums.service.UmsOrganizationService;
import com.kingmed.kmss.common.dto.admin.ums.OrganizationDTO;
import com.kingmed.kmss.common.dto.admin.ums.UmsKmcsOrgDTO;
import com.kingmed.kmss.common.utils.BeanCopyUtils;
import com.kingmed.kmss.common.vo.CommonResponse;
import com.kingmed.kmss.common.vo.CommonResponseHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * KMCS子公司名称及代码 前端控制器
 * </p>
 *
 * @author wyl
 * @since 2023-03-17
 */
@RestController
@RequestMapping("/ums/kmcs/org")
@Api(tags = "ums KMCS子公司名称及代码")
public class UmsKmcsOrgController {

    @Autowired
    private UmsOrganizationService orgService;
    @Autowired
    private UmsKmcsOrgService umsKmcsOrgService;

    @ApiOperation("根据orgId取详情")
    @GetMapping(value = "/detail")
    public CommonResponse<UmsKmcsOrgDTO> getItem(@RequestParam String orgId) {
        UmsKmcsOrg org = umsKmcsOrgService.getBaseMapper().selectOne(
                new QueryWrapper<UmsKmcsOrg>().lambda()
                        .and(Wrapper -> Wrapper.eq(UmsKmcsOrg::getOrgId, orgId).or().eq(UmsKmcsOrg::getKmcsCode, orgId))
        );
        return CommonResponseHelper.success(BeanCopyUtils.copy(org, UmsKmcsOrgDTO.class));
    }

    @ApiOperation("列表")
    @GetMapping(value = "/list")
    public CommonResponse<List<UmsKmcsOrgDTO>> list(@RequestParam(required = false) Integer level) {
        List<UmsKmcsOrg> orgs = umsKmcsOrgService.list(
                new QueryWrapper<UmsKmcsOrg>().lambda()
                        .eq(level != null, UmsKmcsOrg::getOrgLevel, level)
                        .orderByAsc(UmsKmcsOrg::getSort));
        return CommonResponseHelper.success(BeanCopyUtils.copyList(orgs, UmsKmcsOrgDTO.class));
    }

    @ApiOperation("列表（根据level批量）")
    @PostMapping(value = "/listByLevels")
    public CommonResponse<List<UmsKmcsOrgDTO>> listByLevels(@RequestBody List<Integer> levels) {
        return CommonResponseHelper.success(umsKmcsOrgService.listByLevels(levels));
    }

    @ApiOperation("用户ID获取全部子公司：有权限看到的所有子公司")
    @GetMapping(value = "/getByAdminId")
    public CommonResponse<List<OrganizationDTO>> getSubsidiaryByAdminId(@RequestParam("adminId") Long adminId) {
        List<UmsKmcsOrg> orgList = umsKmcsOrgService.list();
        Map<Long, String> orgMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(orgList)) {
            orgList.forEach(item -> orgMap.put(item.getOrgId(), item.getOrgShortName()));
        }
        List<OrganizationDTO> organizationDTOS = orgService.getSubsidiaryByAdminId(adminId);
        List<OrganizationDTO> result = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(organizationDTOS)) {
            for (OrganizationDTO organizationDTO : organizationDTOS) {
                if (orgMap.containsKey(organizationDTO.getId())) {
                    organizationDTO.setOrgShortName(orgMap.get(organizationDTO.getId()));
                    result.add(organizationDTO);
                }
            }
        }
        return CommonResponseHelper.success(result);
    }

}
